#!/bin/bash

# GNU AGPLv3

# Interactive install script for wminizer

# Function to prompt the user for input with a default value
prompt_user() {
    local prompt=$1
    local default=$2
    read -p "$prompt [$default]: " input
    echo "${input:-$default}"
}

# Function to display help message
display_help() {
    echo "Usage: $0 [OPTIONS]"
    echo "Options:"
    echo "  -a, --ask         Ask before proceeding with a list of actions."
    echo "  -p, --preview     Print a list of actions without performing them."
    echo "  -i, --interactive Run in interactive mode."
    echo "  -h, --help        Display this help message."
}

# Function to display the list of actions
display_actions() {
    echo "The following actions will be performed:"
    echo "1. Install scripts to $INSTALL_DIR"
    echo "2. Install manpage to $MAN_DIR"
    if [[ "$CONFIGURE_XINITRC" == "y" ]]; then
        echo "3. Configure .xinitrc to start wminizer"
    else
        echo "3. Skip .xinitrc configuration"
    fi
}

# Default values
DEFAULT_INSTALL_DIR="$HOME/bin"
DEFAULT_MAN_DIR="$HOME/.local/share/man/man1"
CONFIGURE_XINITRC="y"

# Parse command-line options
INTERACTIVE=false
ASK=false
PREVIEW=false
while [[ $# -gt 0 ]]; do
    case $1 in
        -a|--ask)
            ASK=true
            shift
            ;;
        -p|--preview)
            PREVIEW=true
            shift
            ;;
        -i|--interactive)
            INTERACTIVE=true
            shift
            ;;
        -h|--help)
            display_help
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            display_help
            exit 1
            ;;
    esac
done

# Determine the mode of operation
if [[ "$INTERACTIVE" == true ]]; then
    INSTALL_DIR=$(prompt_user "Enter the installation directory" "$DEFAULT_INSTALL_DIR")
    MAN_DIR=$(prompt_user "Enter the manpage installation directory" "$DEFAULT_MAN_DIR")
    CONFIGURE_XINITRC=$(prompt_user "Do you want to configure .xinitrc to start wminizer? (y/n)" "$CONFIGURE_XINITRC")
elif [[ "$ASK" == true ]]; then
    display_actions
    read -p "Do you want to proceed? (y/n): " proceed
    if [[ "$proceed" != "y" ]]; then
        echo "Installation aborted."
        exit 0
    fi
elif [[ "$PREVIEW" == true ]]; then
    display_actions
    exit 0
else
    INSTALL_DIR="$DEFAULT_INSTALL_DIR"
    MAN_DIR="$DEFAULT_MAN_DIR"
fi

# Define the source directory (assuming the script is run from the project root)
SRC_DIR="$(pwd)"

# List of scripts to install
SCRIPTS=("wmrotate1" "wmrotate2" "wminizer")

# Function to copy a script to the install directory and make it executable
install_script() {
    local script=$1
    echo "Installing $script..."
    cp "$SRC_DIR/$script" "$INSTALL_DIR/$script"
    chmod +x "$INSTALL_DIR/$script"
    echo "$script installed successfully."
}

# Install each script
for script in "${SCRIPTS[@]}"; do
    install_script "$script"
done

# Install the manpage
MANPAGE="wminizer.1"
echo "Installing manpage $MANPAGE..."
cp "$SRC_DIR/$MANPAGE" "$MAN_DIR/$MANPAGE"
gzip "$MAN_DIR/$MANPAGE"
echo "Manpage installed successfully."

# Configure .xinitrc
if [[ "$CONFIGURE_XINITRC" == "y" ]]; then
    # Create a timestamped backup of the current .xinitrc if it exists
    XINITRC="$HOME/.xinitrc"
    if [ -f "$XINITRC" ]; then
        BACKUP="$XINITRC.$(date +%Y%m%d%H%M%S).bak"
        echo "Backing up existing .xinitrc to $BACKUP..."
        cp "$XINITRC" "$BACKUP"
    fi

    # Append the wminizer command to the .xinitrc file
    echo "exec wminizer" >> "$XINITRC"
    echo ".xinitrc updated to start wminizer."
else
    echo ".xinitrc configuration skipped."
fi

echo "Installation complete. You can now start your X session with wminizer."
